package com.shaoshaohuo.app.view;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.SshApplication;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.ui.MainTabActivity;

public class TopbarView extends RelativeLayout {
	
	public static final String TITLE_MODEL_EC = "田贸e通电商";
	public static final String TITLE_MODEL_SHIPPER = "田贸e通物流";
	public static final int ID_TITLE_INDEX_EC = 0; //电商的序号
	public static final int ID_TITLE_INDEX_SHIPPER = 1;//物流的序号

	private LinearLayout mLeftLayout;
	private ImageView mLeftBackImage;
//	private TextView mLeftText;
	
	private TextView mCenterText;
	private TextView mRightText;
	private ImageView mRightImage;
	private ImageView mCenterImage;
	
	private ImageView mLeftMenuImage;
	private Spinner mCenterModelSpinner;
	
	private Context mContext;
	
	public TopbarView(Context context) {
		super(context);
		initView(context);
	}

	public TopbarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
	}

	public TopbarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}
	
	private void initView(Context context) {
		this.mContext = context;
		
		LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.topbar,this);
		
		mLeftLayout = (LinearLayout) this.findViewById(R.id.layout_topbar_left);
		mLeftBackImage = (ImageView) this.findViewById(R.id.imageview_topbar_left_backImage);
//		mLeftText = (TextView) this.findViewById(R.id.textview_topbar_left_text);
		mCenterText = (TextView) this.findViewById(R.id.textview_topbar_center_text);
		mRightText = (TextView) this.findViewById(R.id.textview_message);
		mRightImage = (ImageView) this.findViewById(R.id.imageview_message);
		mCenterImage = (ImageView) this.findViewById(R.id.imageview_topbar_center_image);
		
		mCenterModelSpinner = (Spinner) this.findViewById(R.id.textview_topbar_center_model);
		//第二步：为下拉列表定义一个适配器，这里就用到里前面定义的list。    
		ArrayAdapter adapter = new ArrayAdapter<String>(mContext, R.layout.spinner_item_title, new String[] {TITLE_MODEL_EC, TITLE_MODEL_SHIPPER});    
        //第三步：为适配器设置下拉列表下拉时的菜单样式。    
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_title_item);    
        //第四步：将适配器添加到下拉列表上    
        mCenterModelSpinner.setAdapter(adapter);
		mLeftMenuImage = (ImageView) this.findViewById(R.id.imageview_topbar_left_menu);
		
		mLeftLayout.setVisibility(View.GONE);
		mRightText.setVisibility(View.GONE);
		mCenterImage.setVisibility(View.GONE);
		mLeftMenuImage.setVisibility(View.GONE);
		mRightImage.setVisibility(View.GONE);
		
		mCenterModelSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(mContext, MainTabActivity.class);
				switch (position) {
				case 0:
					SshApplication.main_state = 2;
					break;
				case 1:
					SshApplication.main_state = 1;
					break;
				}
				mContext.startActivity(intent);
//				((AActivity)mContext).finish();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
	}

	public void setLeftImage(int res){
		mLeftLayout.setVisibility(View.VISIBLE);
		mLeftBackImage.setImageResource(res);
	}

	/**
	 * 设置右图的宽、高
	 * @param width
	 * @param height
	 */
	public void setRightImage(int width, int height) {
		ViewGroup.LayoutParams params = mRightImage.getLayoutParams();
		params.height = height;
		params.width = width;
		mRightImage.setLayoutParams(params);
	}

	public void setLeftView(boolean isBack,boolean isFinish){
		mLeftLayout.setVisibility(View.VISIBLE);
//		mLeftText.setText(text);
		if(isBack){
			mLeftBackImage.setVisibility(View.VISIBLE);
		}else{
			mLeftBackImage.setVisibility(View.GONE);
		}
		if(isFinish){
			mLeftLayout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					((BaseActivity)mContext).finish();
				}
			});
		}
	}
	
//	public void setLeftView(int res,boolean isBack){
//		mLeftLayout.setVisibility(View.VISIBLE);
////		mLeftText.setText(res);
//		if(isBack){
//			mLeftBackImage.setVisibility(View.VISIBLE);
//		}else{
//			mLeftBackImage.setVisibility(View.GONE);
//		}
//	}
	
	public void setCenterText(String text){
		mCenterText.setVisibility(View.VISIBLE);
		mCenterText.setText(text);
	}
	
	public void setCenterText(int res){
		mCenterText.setVisibility(View.VISIBLE);
		mCenterText.setText(res);
	}
	
	/**
	 * @param index	0是电商； 1是物流
	 */
	public void setCenterModel(int index) {
		switch (SshApplication.main_state) {
		case 2:
			SshApplication.main_state = 2;
			index = 0;
			break;
		case 1:
			SshApplication.main_state = 1;
			index = 1;
			break;
		}
		mCenterModelSpinner.setVisibility(View.VISIBLE);
		mCenterModelSpinner.setSelection(index);
	}
	
	public void setRightVisible(boolean visible){
		if(visible){
			mRightText.setVisibility(View.VISIBLE);
		}else{
			mRightText.setVisibility(View.GONE);
		}
//		mRightText.setText(text);
	}
	
	
	public void setRightText(int res){
		mRightText.setVisibility(View.VISIBLE);
		mRightImage.setVisibility(View.GONE);
		mRightText.setText(res);
	}
	public void setRightText(String res){
		if(TextUtils.isEmpty(res)){
			mRightText.setVisibility(View.GONE);
			mRightImage.setVisibility(View.GONE);
		}else{
			mRightText.setVisibility(View.VISIBLE);
			mRightImage.setVisibility(View.GONE);
			mRightText.setText(res);
		}
	}
	
	public void setRightImage(int res){
		mRightImage.setVisibility(View.VISIBLE);
		mRightText.setVisibility(View.GONE);
		mRightImage.setImageResource(res);
	}
	
	public void setCenterImageVisible(){
		mCenterImage.setVisibility(View.VISIBLE);
		mCenterText.setVisibility(View.GONE);
	}
	
	public void setLeftMenuVisible(){
		mLeftBackImage.setVisibility(View.GONE);
		mLeftMenuImage.setVisibility(View.VISIBLE);
	}
	
	public void setLeftClickListener(OnClickListener onClickListener) {
		mLeftLayout.setVisibility(View.VISIBLE);
		if(onClickListener != null){
			mLeftLayout.setOnClickListener(onClickListener);
		}
	}
	
	
	
	
	public void setRightClickListener(OnClickListener onClickListener){
		if(onClickListener != null){
			if(mRightText.getVisibility() == View.VISIBLE){
				mRightText.setOnClickListener(onClickListener);
			}else{
				mRightImage.setOnClickListener(onClickListener);
			}
//			mRightText.getVisibility() == View.VISIBLE?
//					mRightText.setOnClickListener(onClickListener):mRightImage.setOnClickListener(onClickListener);
		}
	}
	
}
